﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Practices.ServiceLocation;
using Pap.Data;
using Pap.Data.Abstraction.Utils;
using Pap.Services;
using Pap.Services.WorkflowEngine;
using Pap.Web.Utils;

namespace Pap.Web.Modules.Loan.ViewModel
{
    /// <summary>
    /// 
    /// </summary>
    public class ReassignLoansViewModel : IViewModel
    {
        /// <summary>
        /// 
        /// </summary>
        ILoanRepository _LoanRepository;
        /// <summary>
        /// 
        /// </summary>
        IWorkflowService _WorkflowService;
        /// <summary>
        /// 
        /// </summary>
        IMembershipRepository _MembershipRepository;

        /// <summary>
        /// Initializes a new instance of the <see cref="ReassignLoansViewModel"/> class.
        /// </summary>
        /// <param name="LoanRepository">The loan repository.</param>
        /// <param name="WorkflowService">The workflow service.</param>
        /// <param name="MembershipRepository">The membership repository.</param>
        public ReassignLoansViewModel(ILoanRepository LoanRepository, IWorkflowService WorkflowService, IMembershipRepository MembershipRepository)
        {
            this._LoanRepository = LoanRepository;
            this._WorkflowService = WorkflowService;
            this._MembershipRepository = MembershipRepository;
        }

        /// <summary>
        /// Gets the loans data.
        /// </summary>
        /// <returns></returns>
        public IEnumerable GetLoansData()
        {
            var loanCreatedLoans = _LoanRepository.FindCustomerLoans(loan => _WorkflowService.GetInStep(Steps.ResearchAndStudy).Contains(loan.LoanID));
            return (from loan in loanCreatedLoans
                    select new
                    {
                        LoanID = loan.LoanID,
                        LoanNumber = loan.LoanNumber,
                        CustomerName = loan.CustomerObj.CustomerName,
                        CustomerSSNID = loan.CustomerObj.SSN,
                        ResponsibleID = loan.ResponsibleID
                    });
        }

        /// <summary>
        /// Gets all responsibles.
        /// </summary>
        /// <returns></returns>
        public IEnumerable GetAllResponsibles()
        {
            var responsibles = _MembershipRepository.FindUsersByPermission(Permissions.ResponsibleWaitingList);
            return from user in responsibles
                   select new
                   {
                       ResponsibleID = user.UserID,
                       ResponsibleName = user.DisplayName
                   };
        }


        /// <summary>
        /// Submits the loans for responsibles.
        /// </summary>
        /// <param name="LoansData">The loans data.</param>
        public void SubmitLoansForResponsibles(Dictionary<Guid, Guid> LoansData)
        {
            using (IUnitOfWork transaction = ServiceLocator.Current.GetInstance<IUnitOfWork>())
            {
                foreach (var item in LoansData)
                {
                    ICustomerLoan loan = _LoanRepository.GetCustomerLoanByID(item.Key);
                    loan.ResponsibleID = item.Value;
                }
                transaction.Commit();
            }
        }
    }
}
